package com.campus.service.impl;

import com.campus.domain.dto.AddUserDTO;
import com.campus.domain.dto.UpdateUserDTO;
import com.campus.domain.dto.UserQueryDTO;
import com.campus.domain.entity.User;
import com.campus.domain.vo.PageResultVO;
import com.campus.domain.vo.UserVO;
import com.campus.mapper.UserMapper;
import com.campus.service.IAdminUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminUserServiceImpl implements IAdminUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public PageResultVO<UserVO> getUserList(UserQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getPage() - 1) * queryDTO.getSize();
        
        // 查询用户列表
        List<User> users = userMapper.selectUserList(
            queryDTO.getUsername(),
            offset,
            queryDTO.getSize()
        );
        
        // 转换为VO
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<UserVO> voList = users.stream().map(user -> {
            UserVO vo = new UserVO();
            vo.setId(user.getId());
            vo.setUserName(user.getUserName());
            vo.setAvatar(user.getAvatar());
            vo.setEmail(user.getEmail());
            vo.setPhone(user.getPhone());
            vo.setUserType(user.getUserType());
            vo.setCreateTime(user.getCreateTime() != null ? user.getCreateTime().format(formatter) : null);
            vo.setUpdateTime(user.getUpdateTime() != null ? user.getUpdateTime().format(formatter) : null);
            return vo;
        }).collect(Collectors.toList());
        
        // 查询总数
        Long total = userMapper.countUserTotal(queryDTO.getUsername());
        
        return new PageResultVO<>(voList, total, queryDTO.getPage(), queryDTO.getSize());
    }

    @Override
    @Transactional
    public void addUser(AddUserDTO dto) {
        // 检查用户名是否已存在
        Long count = userMapper.count(dto.getUserName());
        if (count > 0) {
            throw new RuntimeException("用户名已存在");
        }
        
        // 创建用户对象
        User user = new User();
        user.setUserName(dto.getUserName());
        user.setPassword(passwordEncoder.encode(dto.getPassword()));
        user.setEmail(dto.getEmail());
        user.setPhone(dto.getPhone());
        user.setUserType(dto.getUserType() != null ? dto.getUserType() : 1);
        user.setAvatar("https://tlias-0626.oss-cn-hangzhou.aliyuncs.com/2025/06/86dff275-7fc9-45a5-b31a-2b97b96ddb39.png");
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        
        // 保存用户
        userMapper.save(user);
    }

    @Override
    @Transactional
    public void updateUser(Long id, UpdateUserDTO dto) {
        // 检查用户是否存在
        User user = userMapper.findById(id);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 更新用户信息
        user.setEmail(dto.getEmail());
        user.setPhone(dto.getPhone());
        if (dto.getUserType() != null) {
            user.setUserType(dto.getUserType());
        }
        user.setUpdateTime(LocalDateTime.now());
        
        userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public void deleteUser(Long id) {
        // 检查用户是否存在
        User user = userMapper.findById(id);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 删除用户
        userMapper.deleteById(id);
    }
}
